﻿using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class ResetPanel : MonoBehaviour
{
    private Button btn_Yes;
    private Button btn_No;
    private Image img_Bg;
    private GameObject dialog;

    private void Awake()
    {
        EventCenter.AddListener(EventDefine.ShowResetPanel,Show);
        img_Bg = transform.Find("bg").GetComponent<Image>();
        btn_Yes = transform.Find("Dialog/btn_Yes").GetComponent<Button>();
        btn_Yes.onClick.AddListener(OnYesButtonClick);
        btn_No = transform.Find("Dialog/btn_No").GetComponent<Button>();
        btn_No.onClick.AddListener(OnNoButtonClick);
       dialog = transform.Find("Dialog").gameObject;

        img_Bg.color = new Color(img_Bg.color.r, img_Bg.color.g, img_Bg.color.b, 0);
        dialog.transform.localScale = Vector3.zero;
        gameObject.SetActive(false);
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener(EventDefine.ShowResetPanel,Show);
    }
    private void Show() 
    {
        gameObject.SetActive(true);
        img_Bg.DOColor(new Color(img_Bg.color.r, img_Bg.color.g, img_Bg.color.b, 0.3f),0.3f);
        dialog.transform.DOScale(Vector3.one,0.3f);
    }
    /// <summary>
    /// 监听YES按钮点击
    /// </summary>
    private void OnYesButtonClick() 
    {
        GameManager.Instance.ResetData();
        //重新加载当前场景
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);//当前场景对应下标
    }
    /// <summary>
    /// 监听NO按钮点击
    /// </summary>
    private void OnNoButtonClick()
    {
        img_Bg.DOColor(new Color(img_Bg.color.r, img_Bg.color.g, img_Bg.color.b, 0), 0.3f);
        //委托 - 动画播放完，界面隐藏
        dialog.transform.DOScale(Vector3.zero, 0.3f).OnComplete(()=>
        {
            //广播事件码--音效
            EventCenter.Broadcast(EventDefine.PlayClickAudio);
            gameObject.SetActive(false);
        });
    }

}
